<template>
	<view class="columnbox wdh-100">

		<image src="/static/logo.png" mode="aspectFill" class="logo"></image>
		<view class="rowbox name">用户端</view>
		<button class="rowbox log-btn def-bgcol" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
			withCredentials="true">立即登录</button>
		<view class="rowbox no-log" @click="cancelLog">暂不登录</view>
		<label class="rowbox wdh-100 radio-box" @click="agreeflag=!agreeflag">
			<radio color="#00ab47" style="transform: scale(0.7);" value="r1" :checked="agreeflag" />
			我已阅读并同意
			<navigator url="">
				<text class="def-col">《用户协议》</text>
			</navigator> 、
			<navigator url="">
				<text class="def-col">《隐私协议》</text>
			</navigator>
		</label>
		<u-popup @close="phoneshow=false" :show="phoneshow" :duration="100" round="10rpx" mode="center">
			<view class="slot-content columnbox">
				<!-- <view class="loginfobox rowbox spb">
					<view class="title">头像</view>
					<view class="rowbox">
						<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						</button>
						<image v-if="avatarUrl==''" class="avatar" src="/static/img/tx.png"> </image>
						<image v-else class="avatar" :src="avatarUrl"></image>
					</view>
				</view> -->
				<image src="/static/img/logo.png" mode="aspectFill" class="phone-logo">
				</image>
				<view class="rowbox name">用户端</view>
				<button type="primary" class="login-btn" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber">绑定手机号</button>

			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				agreeflag: false,
				phoneshow: false,
				avatarUrl: '',
				nickname: '',
				// 扫码信息
				state: 0,
				id: 0
			}
		},
		onLoad(options) {
			if (options.state) {
				this.state = options.state * 1
				this.id = options.id * 1
			}
			uni.showLoading({
				title: '加载中'
			})
			uni.login({
				provider: 'weixin',
				success: (res) => {
					this.$req.post('/user/userlogin', {
						code: res.code,
						// pid: ''
					}).then(res => {
						uni.hideLoading()
						uni.setStorageSync('memberid', res.data.data.id)
						if (res.data.data.phone == '') {} else {
							// 获取个人信息
							this.$req.post('/user/userinfo', {
								memberid: res.data.data.id
							}).then(res => {
								uni.setStorageSync('userinfo', res.data.data)
								uni.setStorageSync('uid', res.data.data.id)
								this.logEasemob()
								this.$store.dispatch('actionsHandleUserInfoChange', res
									.data.data);
								this.$store.dispatch('actionsHandleIsLoginChange',
									true);
								if (res.data.data.id == 0) {
									if (this.state == 0) {
										uni.redirectTo({
											url: '/pages/editinfo/editinfo'
										})
									} else {
										uni.redirectTo({
											url: '/pages/editinfo/editinfo?state=' +
												this.state +
												'&id=' + this.id
										})
									}

								} else {
									if (this.state == 0) {
										uni.navigateBack()
									} else {
										if (this.state == 1) {
											this.$req.post('/user/goodorderbd', {
												id: this.id * 1,
												userid: uni.getStorageSync('uid')
											}).then(res => {

												uni.navigateTo({
													url: '/packageA/chatInfo/goods?id=' +
														this.id
												})
											})
										} else if (this.state == 2) {
											this.$req.post('/user/yaofangxybd', {
												id: this.id * 1,
												userid: uni.getStorageSync('uid')
											}).then(res => {
												uni.redirectTo({
													url: '/packageA/chatInfo/xiyao?type=xiyao&id=' +
														res.data.data
												})
											})
										} else if (this.state == 3) {
											this.$req.post('/user/patientadd', {
												id: this.id * 1,
												uid: uni.getStorageSync('uid')
											}).then(res => {
												// this.$req.post('/user/siyu', {
												// 	id: options.query.id * 1,
												// 	uid: uni.getStorageSync(
												// 		'uid')
												// }).then(res => {})
												uni.hideLoading()
												uni.redirectTo({
													url: '/pages/doctor/detail?id=' +
														this.id * 1
												})
											})

										}
									}
								}
							})
						}
					})
				},
				fail: (err) => {
					console.log('登录失败', err)
				}
			})
		},
		methods: {
			// 登录环信即时通讯
			logEasemob() {
				this.$req.post('/chat/userToken', {
					memberid: uni.getStorageSync('memberid')
				}).then(res => {
					getApp().globalData.conn
						.registerUser({
							/** 用户 ID。 */
							username: uni.getStorageSync('memberid'),
							/** 密码。 */
							password: uni.getStorageSync('memberid')
						}).then((res) => {
							console.log('注册==========', res)
						});
					// 登录
					setTimeout(() => {
						getApp().globalData.conn
							.open({
								user: uni.getStorageSync('memberid') + '',
								accessToken: res.data.data,
							})
							.then(() => {
								console.log("========login success");
								// 登录状态
							})
							.catch((reason) => {
								console.log("=======login fail", reason);
							});
					}, 1000)
				})

			},

			subscribeMessage() {
				uni.getSetting({
					withSubscriptions: true,
					success(res) {
						console.log(res)
						if (!res.subscriptionsSetting.mainSwitch) {
							// uni.openSetting({
							// 	success(res) {
							// 		console.log('打开设置页', res.authSetting);
							// 	}
							// })
						} else {
							uni.requestSubscribeMessage({
								tmplIds: ['WKJuN8gX7gneG0IE4VVs9DbyyMQEgjQLOdPesRCVSjI',
									'nmfwtVizTQOL3ZBqlMlsj7tv1BWIv0SNBLuFpevuRkA'
								],
								success(res) {
									console.log('requestSubscribeMessage 订阅信息', res);
									if (res['WKJuN8gX7gneG0IE4VVs9DbyyMQEgjQLOdPesRCVSjI'] ==
										"accept") { // 用户点击确定后
										console.log('用户订阅点击确定按钮');
										// 后端接口
									} else {
										console.log('拒绝，不会再弹出弹框 只能去设置页');
										uni.showModal({
											title: '您未开启消息订阅',
											content: '为了给您提供更好的服务，请您授权消息订阅',
											success: res2 => {
												if (res2.confirm) {
													uni.openSetting({
														success(res) {
															console.log('打开设置页', res
																.authSetting);
														}
													})
												} else {
													console.log('决绝')
												}
											}
										})

									}
								},
								fail(errMessage) {
									console.log("订阅消息 失败 ", errMessage);
								},
								complete: errMessage => {

								}
							})
						}
					},
				})
			},
			cancelLog() {
				uni.navigateBack()
			},
			showNotice() {
				if (this.nickname == '') {
					uni.showToast({
						title: '请输入昵称',
						icon: 'none'
					})
				}
			},
			getPhoneNumber(e) {
				uni.showLoading({
					title: '加载中'
				})
				let data = {
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					uid: uni.getStorageSync('memberid')
				}
				this.$req.post('/user/phoneJm', {
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					uid: uni.getStorageSync('memberid')
				}).then(res => {
					if (res.data.code == 0) {
						// 获取个人信息
						this.$req.post('/user/userinfo', {
							memberid: uni.getStorageSync('memberid')
						}).then(res => {
							uni.setStorageSync('userinfo', res.data.data)
							uni.setStorageSync('uid', res.data.data.id)
							this.logEasemob()
							this.$store.dispatch('actionsHandleUserInfoChange', res
								.data.data);
							this.$store.dispatch('actionsHandleIsLoginChange',
								true);
							uni.showModal({
								title: '登录成功',
								success: (resp) => {
									this.subscribeMessage()

									if (res.data.data.id == 0) {
										if (this.state == 0) {
											uni.redirectTo({
												url: '/pages/editinfo/editinfo'
											})
										} else {
											uni.redirectTo({
												url: '/pages/editinfo/editinfo?state=' +
													this.state +
													'&id=' + this.id
											})
										}

									} else {
										if (this.state == 0) {
											uni.navigateBack()
										} else {
											if (this.state == 1) {
												this.$req.post('/user/goodorderbd', {
													id: this.id * 1,
													userid: uni.getStorageSync('uid')
												}).then(res => {
													uni.navigateTo({
														url: '/packageA/chatInfo/goods?id=' +
															this.id
													})
												})
											} else if (this.state == 2) {
												this.$req.post('/user/yaofangxybd', {
													id: this.id * 1,
													userid: uni.getStorageSync('uid')
												}).then(res => {
													uni.redirectTo({
														url: '/packageA/chatInfo/xiyao?type=xiyao&id=' +
															res.data.data
													})
												})
											} else if (this.state == 3) {
												uni.redirectTo({
													url: '/pages/doctor/detail?id=' +
														this.id * 1
												})
											}
										}
									}


								}
							})
						})

					} else {
						uni.hideLoading()
						uni.showModal({
							title: '授权手机号失败',
							content: '请稍后再试'
						})
					}
				})
			},
			getUserProfile() {
				if (this.agreeflag) {
					uni.showLoading({
						title: '正在登录'
					})

				} else {
					uni.showModal({
						title: '请阅读同意《用户协议》、《隐私协议》'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.logo {
		margin-top: 70rpx;
		width: 150rpx;
		height: 150rpx;
		border-radius: 100rpx;
	}

	.name {
		margin-top: 20rpx;
	}

	.log-btn {
		width: 600rpx;
		border-radius: 100rpx;
		margin-top: 140rpx;
		height: 100rpx;
		color: #fff;
	}

	.no-log {
		height: 100rpx;
		width: 600rpx;
		border-radius: 100rpx;
		margin-top: 40rpx;
		font-size: 30rpx;
		color: #2e2e2f;
		border: #a2a2a2 solid 1px;
	}

	.radio-box {
		margin-top: 40rpx;
	}

	.loginfobox {
		width: 90%;
		margin-left: 5%;
		position: relative;
		padding: 40rpx 40rpx;
		border-bottom: #f2f6f9 solid 1px;
		font-size: 28rpx;

		input {
			text-align: right;
			font-size: 28rpx;
		}

		image {
			width: 70rpx;
			height: 70rpx;
			border-radius: 100rpx;
		}
	}

	.avatar-wrapper {
		width: 200rpx;
		height: 70rpx;
		position: absolute;
		right: 40rpx;
		opacity: 0;
	}

	.title {
		font-size: 28rpx;
		color: #000;
		font-weight: normal;
	}

	.slot-content {
		width: 500rpx;
		padding: 40rpx 0;
	}

	.login-btn {
		width: 400rpx;
		margin-top: 40rpx;
		background: #57bd6a;
	}

	.phone-logo {
		width: 100rpx;
		height: 100rpx;
		border-radius: 100rpx;
	}
</style>